<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    @register="register"
    @ok="handleSubmit"
    :okAuth="'m8:order:flow:bcQus:edit'"
    width="40%"
  >
    <template #title>
      <span v-if="isShow">
        编程反馈 【{{ titleInfo.orderPic.picno + '|' + titleInfo.orderPic.picname }}】
      </span>
      <span v-else> 编辑反馈记录 </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'WorkCalendarModal',
  });
</script>
<script setup lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { officeTreeData } from '/@/api/sys/office';
  import { bcQusForm, bcQusSave } from '/@/api/machine/bc/bcQus';

  const emit = defineEmits(['success', 'register']);
  const { showMessage } = useMessage();
  // import { DatePicker } from 'ant-design-vue';
  let record = ref<any>();
  // 显示信息
  let titleInfo = ref<any>({
    id: '',
    orderPic: {
      id: '',
      picno: '',
      picname: '',
    },
  });

  let isShow = ref<boolean>(true);

  const inputFormSchemas: FormSchema[] = [
    {
      label: '流程ID',
      field: 'flowId',
      component: 'Input',
      componentProps: {},
      required: true,
      show: false,
    },
    {
      label: '图纸ID',
      field: 'picId',
      component: 'Input',
      componentProps: {},
      required: true,
      show: false,
    },
    {
      label: '提交人',
      field: 'manCode',
      fieldLabel: 'manName',
      component: 'TreeSelect',
      componentProps: {
        api: officeTreeData,
        params: { isLoadUser: true, userIdPrefix: '', isAll: true, isShowCode: true },
        canSelectParent: false,
        allowClear: true,
      },
      required: true,
    },
    {
      label: '问题描述',
      field: 'remarks',
      component: 'InputTextArea',
      required: true,
      componentProps: {
        placeholder: '请填写本次反馈的详细说明',
        maxlength: 1000,
        rows: 10,
      },
    },
  ];

  const [register, { closeDrawer, setDrawerProps }] = useDrawerInner(async (data) => {
    await resetFields();
    if (data.task) {
      titleInfo.value = {
        orderPic: {
          id: data.task.orderPic.id,
          picno: data.task.orderPic.picno,
          picname: data.task.orderPic.picname,
        },
      };
    } else {
      isShow.value = false;
      titleInfo.value.id = data.id;
      titleInfo.value.orderPic.id = data.picId;
    }
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await bcQusForm({ id: titleInfo.value.id, picId: titleInfo.value.orderPic.id });
    record.value = res.m8BcQus || {};
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      console.log(data);
      const res = await bcQusSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
<style scoped></style>
